﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////

#pragma once
#include "WeakPtr.h"
#include <list>
namespace gdmp {
    namespace api {
        /// <summary>
        /// 管理接口等托管代理对象
        /// </summary>
        template<typename T>
        class APIDataGuard
        {
        public:
            void add(T* ptr)
            {
                m_data.emplace_back(ptr);
            }
            //void remove(T* ptr)
            //{
            //    m_data.remove_if([ptr](const auto& item) {return !item || item.Get() == ptr; });
            //}

            ~APIDataGuard()
            {
                for (auto&& ptr : m_data)
                {
                    if (ptr)
                    {
                        auto p = ptr.Get();
                        ptr = nullptr;
                        delete p;
                        p = nullptr;
                    }
                }

                m_data.clear();
            }
        private:
            std::list<gcmp::WeakPtr<T>> m_data;
        };
    }
}
